<?php
class UserNewsDABase extends AbstractDbObject {
	/**
	 * Table name
	 *
	 * @var string
	 */
	protected $tableName = 'user_news';
	
	/**
	 * Table alias
	 *
	 * @var string
	 */
	protected $tableAlias = 'user_news';
	
	/**
	 * Create a page
	 *
	 * @access public
	 *
	 * @param UserNewsEntity $userNewsEntity: object
	 *
	 * @return primary key has just been inserted
	 */
	public function create($userNewsEntity) {
		try {
			// Get db object
			$dbWriter = $this->getDbWriter();
			
			// Fetch array
			$data = array (					
					'title' => $userNewsEntity->title,
					'body' => $userNewsEntity->body,
					'picture' => $userNewsEntity->picture,			
					'metaTitle' => $userNewsEntity->metaTitle, 
					'metaDescription' => $userNewsEntity->metaDescription,
					'metaKeywords' => $userNewsEntity->metaKeywords,
					'ownerId' => $userNewsEntity->ownerId,
					'categoryId' => $userNewsEntity->categoryId,					
					'viewCount' => $userNewsEntity->viewCount,
					'status' => $userNewsEntity->status, 
					'createdDate' => $userNewsEntity->createdDate, 
					'modifiedDate' => $userNewsEntity->modifiedDate
			);
			
			// Insert into database
			$dbWriter->insert($this->tableName, $data);
			
			// Return primary key has just created
			return $dbWriter->lastInsertId($this->tableName, 'id');
		}
		catch (Exception $ex) {
			throw $ex;
		}
	}
	
	/**
	 * Update a userNews
	 *
	 * @access public
	 *
	 * @param UserNewsEntity $userNewsEntity: object
	 *
	 * @return bool true if update success otherwise return false
	 */
	public function update($userNewsEntity) {
		try {
			// Get db object
			$dbWriter = $this->getDbWriter();
			
			//echo $userNewsEntity->picture;
			//die();
			
			// Fetch array
			$data = array (					
					'title' => $userNewsEntity->title,
					'body' => $userNewsEntity->body,
					'picture' => $userNewsEntity->picture,			
					'metaTitle' => $userNewsEntity->metaTitle, 
					'metaDescription' => $userNewsEntity->metaDescription,
					'metaKeywords' => $userNewsEntity->metaKeywords,
					'ownerId' => $userNewsEntity->ownerId,
					'categoryId' => $userNewsEntity->categoryId,					
					'viewCount' => $userNewsEntity->viewCount,
					'status' => $userNewsEntity->status, 
					'createdDate' => $userNewsEntity->createdDate, 
					'modifiedDate' => $userNewsEntity->modifiedDate
			);
			
			// Update userNews
			$idQoute = $dbWriter->quote($userNewsEntity->id);
			$where[] = "id = $idQoute";
			
			// Execute query
			$numRows = $dbWriter->update($this->tableName, $data, $where);
			if ($numRows > 0) {
				return true;
			}
			else {
				return false;
			}
		}
		catch (Exception $ex) {
			throw $ex;
		}
	}
	
	/**
	 * Delete a userNews by Id
	 *
	 * @access public
	 *
	 * @param int $id: Id of userNews
	 *
	 * @return bool whether delete action is success or not
	 */
	public function del($id) {
		try {
			// Get db object
			$dbWriter = $this->getDbWriter();
			
			// Update userNews
			$idQuote = $dbWriter->quote($id);
			
			// Execute query
			$rowCount = $dbWriter->delete($this->tableName, "id = $idQuote");
			
			// Return boolean result
			if ($rowCount > 0) {
				return true;
			}
			else {
				return false;
			}
		}
		catch (Exception $ex) {
			throw $ex;
		}
	}
	
	/**
	 * Get UserNewsEntity object by Id of the userNews
	 *
	 * @access public
	 *
	 * @param int $id: Id of userNews
	 *
	 * @return UserNewsEntity object
	 */
	public function getById($id) {
		// Get db object
		$dbWriter = $this->getDbWriter();
		
		// Add quote
		$idQuote = $dbWriter->quote($id);
		
		// Query for get userNews by Id
		$query = "
			SELECT {$this->tableName}.*
			FROM {$this->tableName}
			WHERE {$this->tableName}.id = $idQuote
		";
		$result = $dbWriter->fetchRow($query);
		if (is_null($result) || !is_array($result)) {
			
			// Id of the userNews does not exist
			return null;
		}
		$userNewsEntity = new UserNewsEntity($result);
		
		// Return UserNewsEntity object
		return $userNewsEntity;
	}
	
	/**
	 * Get all rows of userNews table with search criterias
	 *
	 * @access public
	 *
	 * @param array $arrElement: array of search criterias
	 * @param string $orderBy: Field and the way ordered (ASC or DESC)
	 * @param int $begin: Begin item
	 * @param int $limit: Number rows to query
	 * @param string $where: added where string
	 *
	 * @return array of userNewsEntity object
	 */
	public function getItems($arrElement, $orderBy = null, $begin = null, $limit = null, $where = null, $having = null) {
		// Get db object
		$dbWriter = $this->getDbWriter();
		
		// Query for get all items of user deposit with search criterias
		$query = "
			SELECT {$this->tableName}.*
			FROM {$this->tableName} ";
		
		// Add where
		$whereTmp = "";
		$whereClause = $this->buildWhereClause($arrElement, $dbWriter, $this->tableName);
		$concat = !empty($whereClause) && !empty($whereTmp) ? ' AND ' : '';
		if (empty($whereClause) && !empty($whereTmp)) {
			$whereClause = ' WHERE ';
		}
		$query .= " {$whereClause}{$concat}{$whereTmp} ";
		
		if (!empty($where)) {
			$query .= " AND " . $where;
		}
		
		if (!empty($having)) {
			$query .= " HAVING " . $having;
		}
		
		// Add order By
		$query .= $this->buildOrderClause($orderBy);
		
		// Add begin and Limit
		$query .= $this->buildLimitClause($begin, $limit);
		
		// Execute query
		$result = $dbWriter->fetchAll($query);
		
		$buffer = null;
		foreach ($result as $element) {
			$userNewsEntity = new UserNewsEntity($element);
			
			// Add UserNewsEntity into result
			$buffer[] = $userNewsEntity;
		}
		return $buffer;
	}
	
	/**
	 * Get total rows of userNews
	 *
	 * @access public
	 *
	 * @param array $arrElement: array of search criterias
	 *
	 * @return int total: total rows of userNews
	 */
	public function getTotals($arrElement) {
		// Get db object
		$dbWriter = $this->getDbWriter();
		
		// Query for get total records in userNews table
		$query = "
			SELECT COUNT(*) as total
			FROM {$this->tableName} ";
		// Add where
		$query .= $this->buildWhereClause($arrElement, $dbWriter);
		
		// Execute query
		$result = $dbWriter->fetchRow($query);
		
		if (!is_null($result)) {
			return (int) $result['total'];
		}
		
		// Return no record
		return 0;
	}
}